import { DateFormat, DateFormatList } from '@/types'

/** 日期字符串格式化 */
export default function formatDateString(val: string, format: DateFormat) {
  val = val.replace(/[^\d]+/g, '')

  if (!val) return ''

  const date = {
    yyyy: val.slice(0, 4),
    MM: val.slice(4, 6),
    dd: val.slice(6, 8)
  }

  val = (DateFormatList.find(item => item.value === format) || DateFormatList[0]).format

  Object.entries(date).forEach(([k, v]) => {
    val = val.replace(k, v)
  })

  return val
}
